package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.db.DBVideoDownloadManager;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity.VideoPartEntity;

/**
 * 视频详情列表适配器
 * 
 * @author dhb 20141015
 * 
 */
public class VideoPartsAdapter extends BaseAdapter {

	private Context context;
	private List<VideoPartEntity> myList;
	private DBVideoDownloadManager dbVideoDownloadManager;

	public VideoPartsAdapter(Context context, List<VideoPartEntity> myList) {
		this.context = context;
		this.myList = myList;
		dbVideoDownloadManager = new DBVideoDownloadManager(context);
	}

	public int getCount() {
		return myList.size();
	}

	public Object getItem(int position) {
		return myList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_video_part_layout, null);
			holder.name = (TextView) convertView.findViewById(R.id.part_name);
			holder.imgDownload = (ImageView) convertView
					.findViewById(R.id.img_have_downlaod);
			holder.size = (TextView) convertView.findViewById(R.id.part_size);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final VideoPartEntity partEntity = myList.get(position);
		holder.name.setText(partEntity.part);
		holder.size.setText(partEntity.fileSize + "");
		if (dbVideoDownloadManager.haveDownloadEntity(partEntity)) {
			holder.imgDownload.setVisibility(View.VISIBLE);
		} else {
			holder.imgDownload.setVisibility(View.GONE);
		}

		return convertView;
	}

	class ViewHolder {
		TextView name;// 名称
		ImageView imgDownload;// 是否已下载
		TextView size;// 剧集大小
	}

}